APM.ConfigIntegratedController = Ember.Controller.extend({
	init: function(){
		this._super();
		this.initConfData();
	},

	form : {
		cossEnable : false,
		cossIp : "",
		cossPort : "",
		alarmEnable:true,
		alarmIp : "",
		alarmPort : "",
		certificationEnable : true,
		confType : "2"
	},
	
	initConfData : function(){
		var ctrl = this;
		var form = this.get("form");
		ctrl.store.ajax("GET", "configs/systemConfig", {
			configItem : "integrate"
		}).then(function(response) {
					var configobj = response.configData;
					if(configobj){
						if (configobj.integrate_coss_enabled == "1") {
							ctrl.set("form.cossEnable", true);
						} else {
							ctrl.set("form.cossEnable", false);
						}
						if (configobj.integrate_sso_enabled == "1") {
							ctrl.set("form.certificationEnable", true);
						} else {
							ctrl.set("form.certificationEnable", false);
						}
						if (configobj.integrate_coss_ip) {
							ctrl.set("form.cossIp", configobj.integrate_coss_ip);
						}
						if (configobj.integrate_alert_trap_enabled=="1") {
							ctrl.set("form.alarmEnable", true);
						} else {
							ctrl.set("form.alarmEnable",false);
						}
						if (configobj.integrate_coss_port) {
							ctrl.set("form.cossPort",configobj.integrate_coss_port);
						}
						if (configobj.integrate_alert_port) {
							ctrl.set("form.alarmPort",configobj.integrate_alert_port);
						}
						if (configobj.integrate_alert_ip) {
							ctrl.set("form.alarmIp", configobj.integrate_alert_ip);
						}
					}	
				});
	},
	
	inpDisabled : Ember.computed(function() {
		return true;
	}),
	 	 
	// 禁用远程inp输入框
		apmServerEnableChange : Ember.observer("form.alarmEnable", function() {
			var alarmEnableVal = this.get("form.alarmEnable");
			if (alarmEnableVal=="1"||alarmEnableVal==true) {
				this.set("form.alarmIp","");
				this.set("form.alarmPort","");
				this.set("inpDisabled", true);
			} else {
				this.set("inpDisabled", false);
			}
		}),
	
	checkFormValid: function(){
		var form = this.get("form");
		this.set("saveResult", null);
//		if(form.cossIp){
//			验证由数字、26个英文字母或者下划线组成的字符串
//			var reg0=new RegExp("^[0-9a-zA-Z_]{1,}$");
//			if(!reg0.test(form.cossIp)){
//				this.set("formTip", "对不起，您输入的COSS服务IP地址非法，请重新输入!");
//				   return false;
//			}
//			var cip=form.cossIp;
//			var tempIp=cip.replace(".","1");
//			if(){
//				var reg1=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
//				if(!reg1.test(form.cossIp)){  
//					   this.set("formTip", "对不起，您输入的COSS服务IP地址非法，请重新输入!");
//					   return false;
//				} 
//			}
//		}
		
		if(form.cossPort){
			var reg = new RegExp("^[0-9]*$"); 
			var cport=parseInt(form.cossPort);
			if(!(reg.test(form.cossPort) && cport<65536)){  
			   this.set("formTip", "COSS端口输入错误!");
			   return false;
			}  
		}
		if(form.alarmPort){
			var reg1 = new RegExp("^[0-9]*$"); 
			var aport=parseInt(form.alarmPort);
			if(!(reg1.test(form.alarmPort) && aport<65536)){  
			   this.set("formTip", "告警平台端口错误!");
			   return false;
			} 
		}	
		if(form.alarmIp){
			var reg2=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
			if(!reg2.test(form.alarmIp)){  
				   this.set("formTip", "对不起，您输入的告警平台端口IP地址非法，请重新输入!");
				   return false;
			} 
		}
		this.set("formTip", null);
		return true;
	},

	actions : {
		reset : function() {
			this.set("formTip", null);
			this.set("saveResult", null);
		},

		save : function() {
			if( !this.checkFormValid() ){
				return;
			}
			var ctrl = this;
			var form = this.get("form");

			ctrl.set("saveResult", null);

			if (form.cossEnable) {
				ctrl.set("form.cossEnable", "1");
			} else {
				ctrl.set("form.cossEnable", "0");
			}

			if (form.certificationEnable) {
				ctrl.set("form.certificationEnable", "1");
			} else {
				ctrl.set("form.certificationEnable", "0");
			}
			
			if (form.alarmEnable) {
				ctrl.set("form.alarmEnable", "1");
			} else {
				ctrl.set("form.alarmEnable", "0");
			}
			
			this.store.ajax("POST", "configs/systemConfig", {
				cossEnable : form.cossEnable,
				cossIp : form.cossIp,
				cossPort : form.cossPort,
				certificationEnable : form.certificationEnable,
				alarmEnable : form.alarmEnable,
				alarmIp : form.alarmIp,
				alarmPort : form.alarmPort,
				confType : form.confType
			}).then(function(response) {
				// todo: 保存结果返回逻辑
				if (response.status == "success") {
					ctrl.set("saveResult", "保存成功！");
				}
			}, function() {
				ctrl.set("saveResult", "保存异常！");
			});
		}
	}
});